package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.tools.ToolSOAP;

import org.ksoap2.serialization.SoapObject;

import java.util.TreeMap;

/**
 * Created by lkp on 2015/12/4.
 */
public class zcview_detail extends AbActivity {
    EditText code_et, zc_et, qd_et;
    private MyApplication myApplication; //

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_zcview_detail);
        myApplication = (MyApplication) abApplication;//获得我们的应用程序全局变量
        initview();
    }

    //初始化
    private void initview() {
        //读取所有控件
        zc_et = (EditText) findViewById(R.id.zc_et);
        qd_et = (EditText) findViewById(R.id.qd_et);
        zc_et.setEnabled(false);
        qd_et.setEnabled(false);
        code_et = (EditText) findViewById(R.id.code_et);//条码输入窗口
        code_et.setImeOptions(EditorInfo.IME_ACTION_DONE);
        //条码输入的监听
        code_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            int touch_flag = 0;

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (code_et.getText() == null || code_et.getText().toString().equals(""))
                        return false;
                    touch_flag++;
                    if (touch_flag == 2 || actionId == EditorInfo.IME_ACTION_DONE) {
                        qd_et.setText(code_et.getText().toString());
                        input_code();
                        touch_flag = 0;
                    }
                    return true;
                }
                return false;
            }
        });
        ImageView scancode_iv = (ImageView) findViewById(R.id.scancode_iv);//扫描图标
        //进入时，默认打开扫描窗口
        scancode_click(scancode_iv);
    }

    //为窗口填充数据
    public void insert_data(int listrow) {

    }

    //调用照相机扫描
    public void scancode_click(View v) {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), Basenvo_Capture.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putInt("cameratype", 0);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1);
    }

    public boolean input_code() {
        //获取条码扫描内容，采用文本方式返回，并显示
        TreeMap<String, String> properties = new TreeMap<String, String>();
        properties.put("arg0", myApplication.getGs_ztid());
        properties.put("arg1", myApplication.getGs_year());
        properties.put("arg2", code_et.getText().toString());//资产编码
        ToolSOAP.callService(InstorageURL.SERVERURL, InstorageURL.NAME_SPACE, "ZcViewDetail", properties, new ToolSOAP.WebServiceCallBack() {
            @Override
            public void onSucced(SoapObject result) {
                if (result != null) {
                    String res = result.getProperty(0).toString();
                    if (res.equals("anyType{}") || res.equals("")) {
                        zc_et.setText("资产不存在");
                    } else {
                        zc_et.setText(res);
                    }
                } else {
                    zc_et.setText("数据库返回信息错误");
                    Toast.makeText(getApplicationContext(), "数据库返回信息错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(String result) {
                zc_et.setText("数据库返回信息错误");
                Toast.makeText(getApplicationContext(), "数据库返回信息错误", Toast.LENGTH_SHORT).show();
            }
        });
        code_et.selectAll();
        return true;
    }

    //接收返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            //照相机扫描
            case R.layout.basenvo_capture:
                if (data != null) {
                    Bundle bundle_capture = data.getExtras();
                    String sc_code = bundle_capture.getString("result");
                    //显示扫描内容
                    qd_et.setText(sc_code);
                    String st1[] = sc_code.split("\r\n");
                    //20161008 采用新方式，找到第一个非中文字符后，认为剩余字符是合法的条码内容
                    sc_code = Basenvo.StringLoseChinese(st1[0]);
                    //转换完成
                    code_et.setText(sc_code);
                    if (!sc_code.equals("")) {
                        //处理数据
                        input_code();
                    } else {
                        zc_et.setText("资产条码读取失败");
                    }
                }
                break;
            case R.string.camera_turnback://切换摄像头
                Bundle bundle_turnback = data.getExtras();

                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), Basenvo_Capture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtras(bundle_turnback);
                startActivityForResult(intent, 1);
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void back(View v) {
        //返回时增加提示,防止误点
        finish();
    }
}
